<template>
  <div class="login">
    <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form">
      <h3 class="title">{{ title }}</h3>
      <el-form-item prop="phone">
        <el-input
          v-model="loginForm.phone"
          type="text"
          auto-complete="off"
          placeholder="手机号"
        >
          <svg-icon slot="prefix" icon-class="user" class="el-input__icon input-icon"/>
        </el-input>
      </el-form-item>

      <el-form-item prop="password">
        <el-input
          v-model="loginForm.password"
          type="password"
          auto-complete="off"
          placeholder="密码"
          @keyup.enter.native="handleLogin"
        >
          <svg-icon slot="prefix" icon-class="password" class="el-input__icon input-icon"/>
        </el-input>
      </el-form-item>


      <!--      验证码-->
      <el-form-item prop="code" v-if="captchaEnabled">
        <el-input
          v-model="loginForm.code"
          auto-complete="off"
          placeholder="验证码"
          style="width: 63%"
          @keyup.enter.native="handleLogin"
        >
          <svg-icon slot="prefix" icon-class="validCode" class="el-input__icon input-icon"/>
        </el-input>
        <div class="login-code">
          <img
            :src="codeUrl"
            @click="getCode"
            class="login-code-img"
            alt="验证码"
          />
        </div>
      </el-form-item>


      <el-checkbox v-model="loginForm.rememberMe" style="margin:0px 0px 25px 0px;">记住密码</el-checkbox>

      <el-form-item style="width:100%;">
        <el-button
          :loading="loading"
          size="medium"
          type="primary"
          style="width:100%;"
          @click.native.prevent="handleLogin"
        >
          <span v-if="!loading">登 录</span>
          <span v-else>登 录 中...</span>
        </el-button>
        <div style="float: right;" v-if="register">
          <router-link class="link-type" :to="'/register'">立即注册</router-link>
        </div>
      </el-form-item>
    </el-form>


    <!--  底部  -->
    <div class="el-login-footer">
      <span>思途 © 2025.08.14</span>
    </div>

  </div>
</template>

<script>

import {userLogin, getCaptcha} from "@/api/manager/userLogin"


export default {
  name: "Login",
  data() {
    return {
      title: "球馆预约系统登录",
      codeUrl: "",
      loginForm: {
        phone: "13800138001",
        password: "123456",
        rememberMe: false,
        code: "",
        uuid: ""
      },
      loginRules: {
        phone: [
          {required: true, trigger: "blur", message: "请输入您的手机号"}
        ],
        password: [
          {required: true, trigger: "blur", message: "请输入您的密码"}
        ],
        code: [{required: true, trigger: "change", message: "请输入验证码"}]
      },
      loading: false,
      // 验证码开关
      captchaEnabled: false,
      // 注册开关
      register: false,
      redirect: undefined
    }
  },
  created() {
    this.getCode()
  },
  methods: {
    getCode() {
      //TODO
      // 通过API方法获取验证码，确保携带认证信息
      getCaptcha().then(response => {
        if (response.data) {
          // 将返回的图片数据转换为base64格式
          const blob = new Blob([response.data], {type: 'image/png'});
          this.codeUrl = URL.createObjectURL(blob);

        }
      })
    },
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true;

          // 如果启用了验证码，确保验证码不为空
          if (this.captchaEnabled && !this.loginForm.code) {
            this.$message.error("请输入验证码");
            this.loading = false;
            return;
          }

          // 处理记住密码逻辑
          if (this.loginForm.rememberMe) {
            localStorage.setItem("phone", this.loginForm.phone);
            localStorage.setItem("password", this.loginForm.password);
            localStorage.setItem("rememberMe", true);
          } else {
            localStorage.removeItem("phone");
            localStorage.removeItem("password");
            localStorage.removeItem("rememberMe");
          }
          //清理缓存
          localStorage.removeItem("member");
          localStorage.removeItem("token");

          // 调用登录API（需要您根据实际API路径进行调整）
          //回调函数打印用户信息
          userLogin(this.loginForm).then((resp) => {
            // 存储token
            if (resp.data && resp.data.token) {
              console.log(resp.data.token)
              localStorage.setItem("token", resp.data.token);
            }

            // 可选：存储用户信息
            if (resp.data && resp.data.member) {
              localStorage.setItem("member", JSON.stringify(resp.data.member));
            }

            this.$router.push({path: "/manager/userReservation"});
          }).catch(() => {
            this.loading = false;
            // 登录失败时清除可能存在的无效token
            localStorage.removeItem("token");
            // 登录失败时刷新验证码
            if (this.captchaEnabled) {
              this.getCode();
            }
          });
        }
      });
    }

  }
}
</script>


<style rel="stylesheet/scss" lang="scss">
.login {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh; /* 修改为min-height确保至少占满整个视口高度 */
  background-image: url("../../../assets/images/login-background.jpg");
  background-size: cover; /* 保持图片比例并覆盖整个背景 */
  background-position: center; /* 将背景图片居中显示 */
  background-repeat: no-repeat; /* 防止背景图片重复 */
}

.title {
  margin: 0px auto 30px auto;
  text-align: center;
  color: #707070;
}

.login-form {
  border-radius: 6px;
  background: #ffffff;
  width: 400px;
  padding: 25px 25px 5px 25px;
  z-index: 1;

  .el-input {
    height: 38px;

    input {
      height: 38px;
    }
  }

  .input-icon {
    height: 39px;
    width: 14px;
    margin-left: 2px;
  }
}

.login-tip {
  font-size: 13px;
  text-align: center;
  color: #bfbfbf;
}

.login-code {
  width: 33%;
  height: 38px;
  float: right;

  img {
    cursor: pointer;
    vertical-align: middle;
  }
}

.el-login-footer {
  height: 40px;
  line-height: 40px;
  position: fixed;
  bottom: 0;
  width: 100%;
  text-align: center;
  color: #fff;
  font-family: Arial;
  font-size: 12px;
  letter-spacing: 1px;
}

.login-code-img {
  height: 38px;
}
</style>

